package com.huanletao.guava.str;

import com.google.common.base.Splitter;
import org.junit.Test;

import java.util.List;
import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class SplitterDemo {
    @Test
    public void testSplitter(){
        List<String> stringList = Splitter.on("|").splitToList("hello|word");
        assertThat(stringList,notNullValue());
        assertThat(stringList.size(),equalTo(2));
    }

    @Test
    public void testSplitter_OmitEmpty(){
        List<String> stringList = Splitter.on("|").omitEmptyStrings().splitToList("hello|word||||");
        assertThat(stringList,notNullValue());
        assertThat(stringList.size(),equalTo(2));
    }

    @Test
    public void testSplitter_TrimResult(){
        List<String> stringList = Splitter.on("|").trimResults().omitEmptyStrings().splitToList(" hello | word||||");
        assertThat(stringList,notNullValue());
        assertThat(stringList.size(),equalTo(2));
    }

    @Test
    public void testSplitter_FixLeng(){
        List<String> result = Splitter.fixedLength(4).splitToList("aaaabbbbccccdddd");
        assertThat(result,notNullValue());
        assertThat(result.size(),equalTo(4));
        assertThat(result.get(0),equalTo("aaaa"));
    }

    @Test
    public void testSplitter_Limit(){
        List<String> result = Splitter.on("#").limit(3).splitToList("aaaa#bbbb#cccc#dddd");
        assertThat(result,notNullValue());
        assertThat(result.size(),equalTo(3));
        assertThat(result.get(2),equalTo("cccc#dddd"));
    }

    // 转Map
    @Test
    public void testSplitter_SplitToMap(){
        Map<String, String> split = Splitter.on("|").withKeyValueSeparator("=").split("a=b|c=d");
        assertThat(split,notNullValue());
        assertThat(split.size(),equalTo(2));
        assertThat(split.get("a"),equalTo("b"));
    }

    public static void main(String[] args) {
        String str = "2020-12-12";
        List<String> split = Splitter.on("-").splitToList(str);
        System.out.println("split.get(0) = " + split.get(0));
        System.out.println("split.get(0) = " + split.get(1));
        System.out.println("split.get(0) = " + split.get(2));
    }

}
